﻿using _08Net8AspNetCore_MiddleWare.Middlewares;
using _08Net8AspNetCore_MiddleWare.Module;
using Microsoft.AspNetCore.Builder;

namespace _08Net8AspNetCore_MiddleWare
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            #region 1、web主机使用
            //{
            //    // 1、创建WebApplicationBuilder
            //    WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

            //    // 2、创建WebApplication
            //    WebApplication app = webApplicationBuilder.Build();

            //    // 3、运行项目
            //    app.Run();

            //}
            #endregion

            #region 2、web主机使用-端口监听
            //{
            //    // 1、创建WebApplicationBuilder
            //    WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

            //    // 2、创建WebApplication
            //    WebApplication app = webApplicationBuilder.Build();

            //    // 3、运行项目
            //    app.Run("https://localhost:5001");

            //}
            #endregion

            #region 3、web主机使用-多端口监听
            //{
            //    // 1、创建WebApplicationBuilder
            //    WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

            //    // 2、创建WebApplication
            //    WebApplication app = webApplicationBuilder.Build();

            //    // 3、运行项目
            //    app.Urls.Add("https://localhost:5001");
            //    app.Urls.Add("http://localhost:5002");
            //    app.Run();

            //}
            #endregion

            #region 4、web主机使用-动态端口监听(练习不通过)
            {
               // 1、创建WebApplicationBuilder
               //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

               // // 2、创建WebApplication
               // WebApplication app = webApplicationBuilder.Build();

               // // 3、运行项目
               // app.Run();

            }
            #endregion

            #region 5、web主机使用-内置托管主机
            {
                //// 1、创建WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

                /////Kestrel IIS httpSys
                ////webApplicationBuilder.WebHost.UseIIS();
                ////webApplicationBuilder.WebHost.UseHttpSys();
                ////webApplicationBuilder.WebHost.UseKestrel();

                //// 2、创建WebApplication
                //WebApplication app = webApplicationBuilder.Build();

                //// 3、运行项目
                //app.Run();

            }
            #endregion


            #region 6、web主机使用-外网访问  无反应  脚本调试打开https 不能打开http
            {
                //// 1、创建WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

                //// 2、创建WebApplication
                //WebApplication app = webApplicationBuilder.Build();

                //// 3、运行项目
                //app.Run("http://+:7077");

            }
            #endregion

            #region 2.1、浏览器发起 请求 响应字符串
            {
                //// 1、创建WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

                //// 2、内置主机切换
                //webApplicationBuilder.WebHost.UseKestrel();

                //// 2、创建WebApplication
                //WebApplication app = webApplicationBuilder.Build();

                ////app.Run(async (context) =>
                ////{
                ////    await context.Response.WriteAsync("Hello World");
                ////});
                //app.Run(async (context) =>
                //{
                //    await ResposeString(context);
                //});

                //// 3、运行项目
                //app.Run("https://+:7077");
            }
            #endregion

            #region 2.2、浏览器发起 请求 类
            {
                //// 1、创建WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

                //// 2、内置主机切换
                //webApplicationBuilder.WebHost.UseKestrel();

                //// 2、创建WebApplication
                //WebApplication app = webApplicationBuilder.Build();

                //app.Run(async (context) =>
                //{
                //    await ResposeObject(context);
                //});

                //// 3、运行项目
                //app.Run("https://+:7077");
            }
            #endregion
            #region 2.3、浏览器发起 请求 集合类
            //{
            //    // 1、创建WebApplicationBuilder
            //    WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

            //    // 2、内置主机切换
            //    webApplicationBuilder.WebHost.UseKestrel();

            //    // 2、创建WebApplication
            //    WebApplication app = webApplicationBuilder.Build();

            //    app.Run(async (context) =>
            //    {
            //        await ResposeListObject(context);
            //    });

            //    // 3、运行项目
            //    app.Run("https://+:7077");
            //}
            #endregion

            #region 2.4、浏览器发起 不同的请求，得到不同的响应 js html css 集合对象
            {
                /*// 1、创建WebApplicationBuilder
                WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

                // 2、内置主机切换
                webApplicationBuilder.WebHost.UseKestrel();

                // 2、创建WebApplication
                WebApplication app = webApplicationBuilder.Build();

                // 方法1
                //app.Run(async (context) =>
                //{
                //    string path = context.Request.Path.Value;
                //    if (path.Contains("html"))
                //    {
                //        await ResposeHtml(context);
                //    }
                //    else if (path.Contains("JS"))
                //    {
                //        await ResposeJS(context);
                //    }
                //    else if (path.Contains("CSS"))
                //    {
                //        await ResposeCSS(context);
                //    }
                //    else
                //    {
                //        await ResposeListObject(context);
                //    }
                //});

                //方法2
                app.Use(async (context, next) =>
                {
                    string path = context.Request.Path.Value;
                    if (path.Contains("html"))
                    {
                        await ResposeHtml(context);
                    }
                    else
                    {
                        await next();
                    }
                });

                app.Use(async (context, next) =>
                {
                    string path = context.Request.Path.Value;
                    if (path.Contains("js"))
                    {
                        await ResposeJS(context);
                    }
                    else
                    {
                        await next();
                    }
                });

                app.Use(async (context, next) =>
                {
                    string path = context.Request.Path.Value;
                    if (path.Contains("css"))
                    {
                        await ResposeCSS(context);
                    }
                    else
                    {
                        await next();
                    }
                });

                // 3、运行项目
                app.Run("https://+:7077"); */
            }
            #endregion

            #region 2.4、浏览器发起 不同的请求，得到不同的响应 js html css 集合对象  封装
            /*{
                // 1、创建WebApplicationBuilder
                WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

                // 2、内置主机切换
                webApplicationBuilder.WebHost.UseKestrel();

                // 2、创建WebApplication
                WebApplication app = webApplicationBuilder.Build();

                app.UseMiddleware<CSSMiddleWare>();
                app.UseMiddleware<JSMiddleWare>();
                app.UseMiddleware<HtmlMiddleWare>();

                // 3、运行项目
                app.Run("https://+:7077");
            }*/
            #endregion

            #region 2.5、浏览器发起 不同的请求，得到不同的响应 js html css 集合对象  扩展
            {
               /* // 1、创建WebApplicationBuilder
                WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();

                // 2、内置主机切换
                webApplicationBuilder.WebHost.UseKestrel();

                // 2、创建WebApplication
                WebApplication app = webApplicationBuilder.Build();

                app.UseHtml();
                app.UseJS();
                app.UseCSS();

                // 3、运行项目
                app.Run("https://+:7077");*/
            }
            #endregion

            #region 2.5、异常处理
            {
                // 1、创建WebApplicationBuilder
                WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                // 1.1、注册ProductService
                webApplicationBuilder.Services.AddSingleton<StudentService>();

                // 2、内置主机切换
                webApplicationBuilder.WebHost.UseKestrel();

                // 2、创建WebApplication
                WebApplication app = webApplicationBuilder.Build();
                app.UseMiddleware<CSSMiddleWare>();
                app.UseException();

                // 3、运行项目
                app.Run("https://+:7077");
            }
            #endregion
        }

        /// <summary>
        /// 2.1、相应字符串
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static async Task ResposeString(HttpContext context)
        {
           await context.Response.WriteAsync($"Hello World {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")}");
        }


        /// <summary>
        /// 2.2、相应对象
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static async Task ResposeObject(HttpContext context)
        {
            Student student = new Student();
            student.ID = 1;
            student.Name = "FUFU";
            await context.Response.WriteAsJsonAsync(student);
        }

        /// <summary>
        /// 2.3、相应集合对象
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static async Task ResposeListObject(HttpContext context)
        {
            List<Student> students = new List<Student>() {
                new Student() { ID = 1, Name = "FUFU" },
                new Student() { ID = 2, Name = "FUFU2" },
            };
            await context.Response.WriteAsJsonAsync(students);
        }

        /// <summary>
        /// 获取html
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static async Task ResposeHtml(HttpContext context)
        {
            // 1、设置文件类型
            context.Response.ContentType = "text/html";
            // 2、输出路径
            string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Index.html");
            // 3、读取文件内容
            var htmlContent = await File.ReadAllTextAsync(path);
            // 4、相应输出
            await context.Response.WriteAsync(htmlContent);
        }

        /// <summary>
        /// 获取JS
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static async Task ResposeJS(HttpContext context)
        {
            // 1、设置文件类型
            context.Response.ContentType = "text/Javascript";
            // 2、输出路径
            string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Index.js");
            // 3、读取文件内容
            var htmlContent = await File.ReadAllTextAsync(path);
            // 4、相应输出
            await context.Response.WriteAsync(htmlContent);
        }

        /// <summary>
        /// 获取CSS
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static async Task ResposeCSS(HttpContext context)
        {
            // 1、设置文件类型
            context.Response.ContentType = "text/css";
            // 2、输出路径
            string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Index.css");
            // 3、读取文件内容
            var htmlContent = await File.ReadAllTextAsync(path);
            // 4、相应输出
            await context.Response.WriteAsync(htmlContent);
        }
    }
}
